#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GL/glfw3.h>
#include <portaudio.h>
#include "vmath/vectormath_aos.h"

//------------------------------------------------------------------------------
GLFWwindow g_window;
int g_run = 1;
double g_time_current;
float g_time_delta;
//------------------------------------------------------------------------------
void APIENTRY OpenGLDebugOutput(GLenum source, GLenum type, GLuint id,
                                GLenum severity, GLsizei length,
                                const GLchar *message, GLvoid *param) {
  fprintf(stderr, "%s\n", message);
}
//------------------------------------------------------------------------------
void Keyboard(GLFWwindow window, int key, int action) {
  if (key == GLFW_KEY_ESCAPE) g_run = 0;
}
//------------------------------------------------------------------------------
int WindowClose(GLFWwindow window) {
  g_run = 0;
  return 0;
}
//------------------------------------------------------------------------------
void Init() {
}
//------------------------------------------------------------------------------
void Terminate() {
}
//------------------------------------------------------------------------------
void Update() {
}
//------------------------------------------------------------------------------
void Display() {
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glfwSwapBuffers();
}
//------------------------------------------------------------------------------
int main(int argc, char **argv) {
  if (!glfwInit()) {
    fprintf(stderr, "glfwInit failed: %s.\n", glfwErrorString(glfwGetError()));
    exit(EXIT_FAILURE);
  }

  glfwOpenWindowHint(GLFW_WINDOW_RESIZABLE, 0);
  glfwOpenWindowHint(GLFW_RED_BITS, 8);
  glfwOpenWindowHint(GLFW_GREEN_BITS, 8);
  glfwOpenWindowHint(GLFW_BLUE_BITS, 8);
  glfwOpenWindowHint(GLFW_ALPHA_BITS, 8);
  glfwOpenWindowHint(GLFW_DEPTH_BITS, 24);
  glfwOpenWindowHint(GLFW_STENCIL_BITS, 8);
  glfwOpenWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, 1);

  g_window = glfwOpenWindow(800, 600, GLFW_WINDOWED, "eneida", NULL);
  if (!g_window) {
    fprintf(stderr, "glfwOpenWindow failed: %s.\n",
            glfwErrorString(glfwGetError()));
    exit(EXIT_FAILURE);
  }
  if (glewInit() != GLEW_OK) {
    fprintf(stderr, "glewInit failed.\n");
    exit(EXIT_FAILURE);
  }
  glfwSwapInterval(0);

  if (glDebugMessageCallbackARB != NULL) {
    glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
    glDebugMessageControlARB(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, NULL,
                             GL_TRUE);
    glDebugMessageCallbackARB(OpenGLDebugOutput, NULL);
  }

  glfwSetKeyCallback(Keyboard);
  glfwSetWindowCloseCallback(WindowClose);

  Init();

  glfwSetTime(0.0);
  while (g_run) {
    g_time_current = glfwGetTime();

    Update();
    Display();
    glfwPollEvents();
  }

  Terminate();
  glfwTerminate();

  return EXIT_SUCCESS;
}
//------------------------------------------------------------------------------
